$('#footer').load('data/footer.php');
var authCode = function(){
    var ctx = auth_code_canvas.getContext('2d');
    var canvasWidth = parseInt($('#auth_code_canvas').css('width'));
    var canvasHeight = parseInt($('#auth_code_canvas').css('height'));
    /**绘制一个随机的背景颜色——填充一个矩形**/
    ctx.fillStyle = randColor(170,250); //浅色
    ctx.fillRect(0,0,canvasWidth,canvasHeight);
    /**绘制4个随机的字符**/
    var data = 'ABCDEFGHJKMNPQRSTWXY3456789';
    //四个字符串
    var c1 = data[randNum(0,data.length)];
    ctx.fillStyle = randColor(60,160);//文字颜色
    ctx.font = randNum(15,40)+'px SimHei';//文字大小
    ctx.fillText(c1, randNum(0,15), randNum(20,50));
    var c2 = data[randNum(0,data.length)];
    ctx.fillStyle = randColor(60,160);//文字颜色
    ctx.font = randNum(15,40)+'px SimHei';//文字大小
    ctx.fillText(c2, 30+randNum(0,15), randNum(20,50));
    var c3 = data[randNum(0,data.length)];
    ctx.fillStyle = randColor(60,160);//文字颜色
    ctx.font = randNum(15,40)+'px SimHei';//文字大小
    ctx.fillText(c3, 57+randNum(0,15), randNum(20,50));
    var c4 = data[randNum(0,data.length)];
    ctx.fillStyle = randColor(60,160);//文字颜色
    ctx.font = randNum(15,40)+'px SimHei';//文字大小
    ctx.fillText(c4, 85+randNum(0,15), randNum(20,50));
    /**绘制颜色随机的干扰线**/
    for(var i=0; i<10; i++){
        ctx.beginPath();
        ctx.moveTo( randNum(0,120),  randNum(0,50) );
        ctx.lineTo( randNum(0,120),  randNum(0,50) );
        ctx.strokeStyle = randColor(60,160);
        ctx.stroke();
    }
    //产生一个随机的颜色值
    function randColor(min,max){
        var r = Math.floor(Math.random()*(max-min)+min);
        var g = Math.floor(Math.random()*(max-min)+min);
        var b = Math.floor(Math.random()*(max-min)+min);
        return 'rgb('+r+','+g+','+b+')';
    }
    //产生一个随机的整数值
    function randNum(min,max){
        var num = Math.floor(Math.random()*(max-min)+min);
        return num;
    }
    function authCodeReturn(){
        var str = c1+c2+c3+c4;
        return str;
    }
    return authCodeReturn();
}
$(function(){
    var authCodeStr = authCode();
    window.localStorage.removeItem('authCode');
    window.localStorage.setItem('authCode',authCodeStr);
    $('#auth_code_canvas').click(authCode);
    $('#auth_code_canvas').click(function(){
        var authCodeStr = authCode();
        window.localStorage.removeItem('authCode');
        window.localStorage.setItem('authCode',authCodeStr);
    });
    $('#register_btn').click(function(authCodeStr){
        //console.log(authCodeStr);
        var phone = $('#register_phone').val();
        var pwd = $('#register_pwd').val();
        var authCode = window.localStorage.getItem('autnCode');
        if(!protocol_selector.checked){
            $('#protocol_alert').html('点击看看相关协议');
        }else{
            $('#protocol_alert').html('');
        }
        if(!register_phone.value){
            $('#phone_alert').html('手机号码不能为空！');
        }
        if(!register_pwd.value){
            $('#pwd_alert').html('密码不能为空！');
        }
        if(!auth_code.value){
            $('#auth_code_alert').html('验证码不正确！');
        }
        if(!tel_code.value){
            $('#phone_code_alert').html('手机验证码不能为空！');
        }
        if(protocol_selector.checked&&tel_code.value&&register_pwd.value&&register_phone.value&&auth_code.value){
            var authCodeInput = auth_code.value;
            authCodeInput = authCodeInput.toUpperCase();
            var authCode = window.localStorage.getItem('authCode');
            authCode = authCode.toUpperCase();
            if(authCode==authCodeInput){
                $.post('data/register.php',{uPhone:phone,uPassword:pwd},function(txt){
                    if(txt){
                        window.localStorage.removeItem('nubia_user_name');
                        window.localStorage.setItem('nubia_user_name',register_phone.value);
                        setTimeout(function(){
                            location='index.html'
                        },500);
                    }
                });
            }else{
                $('#auth_code_alert').html('验证码不正确！');
            }
        }
    });
    $('#register_phone').focusout(function(){
        if(!$('#register_phone').val()){$('#phone_alert').html('手机号码不能为空！')};
    });
    $('#register_phone').keyup(function(){
        if($('#register_phone').val()){$('#phone_alert').html('')};
    });
    $('#register_pwd').focusout(function(){
        if(!$('#register_pwd').val()){$('#pwd_alert').html('密码不能为空！')};
    });
    $('#register_pwd').keyup(function(){
        if($('#register_pwd').val()){$('#pwd_alert').html('')};
    });
    $('#auth_code').focusout(function(){
        if(!$('#auth_code').val()){$('#auth_code_alert').html('验证码不正确！')};
    });
    $('#auth_code').keyup(function(){
        if($('#auth_code').val()){$('#auth_code_alert').html('')};
    });
    $('#tel_code').focusout(function(){
        if(!$('#tel_code').val()){$('#phone_code_alert').html('手机验证码不能为空！')};
    });
    $('#tel_code').keyup(function(){
        if($('#tel_code').val()){$('#phone_code_alert').html('')};
    });
    $('#protocol_selector').click(function(){
        if(protocol_selector.checked){$('#protocol_alert').html('')}
    });
});
